//
//  CommentViewController.m
//  CommonFrame
//
//  Created by Bang-guo on 15/7/21.
//  Copyright (c) 2015年 guobang. All rights reserved.
//

#import "CommentViewController.h"

@interface CommentViewController ()

@end

@implementation CommentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = self.title;
    self.recArray = [NSMutableArray array];
    self.comArray = [NSMutableArray array];
    [self getDataValue];
    
//    NSLog(@"%@ %@", self.recDetail.photo_s, self.recDetail.text);
    
    self.commentTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 20- 44 - 49) style:UITableViewStylePlain];
    [self.view addSubview:self.commentTableView];
    self.commentTableView.delegate = self;
    self.commentTableView.dataSource = self;
    
    BaseView *headView = [[BaseView alloc] init];
    UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 200)];
    [imageV sd_setImageWithURL:[NSURL URLWithString:self.recDetail.photo_s]];
    [headView addSubview:imageV];
    ;
    BaseLabel *lable = [[BaseLabel alloc] initWithFrame:CGRectMake(10, imageV.frame.size.height + 10, WIDTH - 20, [self heightCell:self.recDetail.text])];
    lable.text = self.recDetail.text;
    [headView addSubview:lable];
    lable.numberOfLines = 0;
    headView.frame = CGRectMake(0, 0, WIDTH, imageV.frame.size.height + lable.frame.size.height + 40);
    headView.backgroundColor = [UIColor whiteColor];
    UIView *aview= [[UIView alloc] initWithFrame:CGRectMake(0, headView.frame.size.height, WIDTH, 1)];
    aview.backgroundColor = [UIColor lightGrayColor];
    [headView addSubview:aview];
    self.commentTableView.tableHeaderView = headView;
    
    //让菊花旋转起来
    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.HUD.labelText = @"正在加载中...";
    [self.HUD show:YES];
    
}

-(void)getDataValue {
    
    NSString *urlStr = [NSString stringWithFormat:@"http://api.breadtrip.com/trips/%@/waypoints/%@/replies/?count=32767", self.uId, self.recDetail.dayId];
//    NSLog(@"urlStr = %@", urlStr);
    [AFNGetDataValue getDataAFN:urlStr block:^(NSMutableDictionary *dataDic) {
        
        
        NSMutableArray *array = [NSMutableArray arrayWithArray:[dataDic objectForKey:@"recommenders"]];
        for (NSMutableDictionary *dic in array) {
            
            Comment *com = [[Comment alloc] init];
            com.avatar_s = [dic objectForKey:@"avatar_s"];
            com.uId = [NSString stringWithFormat:@"%@", [dic objectForKey:@"id"]];
            [self.recArray addObject:com];
            
        }
        
        NSMutableArray *tempArray = [NSMutableArray arrayWithArray:[dataDic objectForKey:@"comments"]];
        for (NSMutableDictionary *dic in tempArray) {
            
            Comment *com = [[Comment alloc] init];
            com.userAvatar_s = [[dic objectForKey:@"user"] objectForKey:@"avatar_s"];
            com.userName = [[dic objectForKey:@"user"] objectForKey:@"name"];
            com.userComment = [dic objectForKey:@"comment"];
            [self.comArray addObject:com];
        }
        
        NSLog(@"%ld %ld", self.recArray.count, self.comArray.count);
        [self.commentTableView reloadData];
        
        
        [self.HUD hide:YES];
    
    }];
    
}



-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 2;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        static NSString *identifier = @"cell";
        CommentTableCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell == nil) {
            
            cell = [[CommentTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
//            cell.textLabel.text = @"0";
        }
        cell.recArray = [NSMutableArray arrayWithArray:self.recArray];
        return cell;
    }
    
    if (indexPath.section == 1) {
        
        static NSString *identifier1 = @"cell1";
        CommentTableCell *cell1 = [tableView dequeueReusableCellWithIdentifier:identifier1];
        if (cell1 == nil) {
            
            cell1 = [[CommentTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier1];
//            cell1.textLabel.text = @"1";
            
            
        }
        Comment *com = self.comArray[indexPath.row];
        cell1.comment = com;
        
        return cell1;
    }
    

    return nil;
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    
    if (section == 0) {
        
        return @"喜欢";
    }
    if (section == 1) {
        
        return @"评论";
    }
    
    return 0;
    
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        
        return 50;
    } else {
        
        return 100;
    }
    
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (section == 0) {
        return 1;
    } else {
        
        return self.comArray.count;
    }
    
    
}

-(CGFloat)heightCell:(NSString *)content {
    //参数1.设置计算内容的宽度
    //参数2.设置计算高度
    //参数3.设置字体大小属性
    //参数4.系统备用参数,设置为nil
    
    //其中宽度一定与消失内容的label宽度一致,否则不准确
    CGSize size = CGSizeMake(WIDTH, 100000);
    
    //字体大小一定与显示内容的label字体大小一致,默认 17
    NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:17.0] forKey:NSFontAttributeName];
    
    CGRect frame = [content boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    
    
    return frame.size.height;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
